import 'dart:math' as math;
import 'package:demo1/view/home.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';


class RadialAnimationDemo extends StatelessWidget {
  static const double minRadius = 32;
  static const double maxRadius = 128;
  static const opacityCurve = const Interval(
      0, 0.75, curve: Curves.fastOutSlowIn);

  static RectTween _createRescTween(Rect begin, Rect end) {
    return MaterialRectArcTween(begin: begin, end: end);
  }

  @override
  Widget build(BuildContext context) {
    timeDilation = 6;
    return Scaffold(
      appBar: AppBar(
        title: Text('radial demo'),
      ),
      body: Container(
        padding: const EdgeInsets.all(32),
        alignment: FractionalOffset.bottomLeft,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            _buildHero(context, 'images/me1.JPG', 'me1'),
            _buildHero(context, 'images/me2.JPG', 'me2'),
            _buildHero(context, 'images/me3.JPG', 'me3'),
          ],
        )
      ),
    );
  }

  Widget _buildHero(BuildContext context, String imageName, String des) {
    return Container(
      width: minRadius * 2,
      height: minRadius * 2,
      child: Hero(
        createRectTween: _createRescTween,
        tag: imageName,
        child: RadialExpansion(
            maxRadius: maxRadius,
          child: Photo(
            photo: imageName,
            onTap: () {
              Navigator.of(context).push(
                  PageRouteBuilder<void> (
                      pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
                        return AnimatedBuilder(
                            animation: animation,
                            builder: (BuildContext context, Widget child) {
                              return Opacity(
                                opacity: opacityCurve.transform(animation.value),
                                child: _buildPage(context, imageName, des),
                              );
                            });
                      }
                  )
              );
            },
          ),
        ),
      ),
    );
  }

 static Widget _buildPage(BuildContext context, String imageName, String des) {
    return Container(
      color: Theme.of(context).canvasColor,
      child: Center(
        child: Card(
          elevation: 8,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              SizedBox(
                width: maxRadius * 2,
                height: maxRadius * 2,
                child: Hero(
                  createRectTween: _createRescTween,
                  tag: imageName,
                  child: RadialExpansion(
                    maxRadius: maxRadius,
                    child: Photo(
                      photo: imageName,
                      onTap: () {
                        Navigator.of(context).pop;
                      },
                    ),
                  ),
                ),
              ),
               Text(
                   des,
                 style: TextStyle(fontWeight: FontWeight.bold),
                 textScaleFactor: 3,
               ),
              const SizedBox(height: 16,)
            ],
          ),
        ),
      ),
    );
 }

}

class Photo extends StatelessWidget {

  final String photo;
  final Color color;
  final VoidCallback onTap;


  Photo({Key key, this.photo, this.color, this.onTap}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Material(
        color: Theme
            .of(context)
            .primaryColor
            .withOpacity(0.25),
        child: InkWell(
          onTap: onTap,
          child: LayoutBuilder(
            builder: (BuildContext context, BoxConstraints size) {
              return Image.asset(
                photo,
                fit: BoxFit.contain,
              );
            },
          ),
        )

    );
  }

}

class RadialExpansion extends StatelessWidget {

  final double maxRadius;
  final clipRectSize;
  final Widget child;

  RadialExpansion({Key key, this.maxRadius, this.child})
      : clipRectSize = 2.0 * (maxRadius / math.sqrt2),
        super(key: key);

  @override
  Widget build(BuildContext context) {
    return ClipOval(
      child: Center(
        child: SizedBox(
          width: clipRectSize,
          height: clipRectSize,
          child: ClipRect(
            child: child,
          ),
        ),
      ),
    );
  }


}